
#include "BoundingBox.h"
#include <float.h>

BoundingBox::BoundingBox()
{
  min = Point(DBL_MAX, DBL_MAX, DBL_MAX);
  max = Point(-DBL_MAX, -DBL_MAX, -DBL_MAX);
}

BoundingBox::BoundingBox(const Point& corner1, const Point& corner2)
{
	min = Min(corner1, corner2);
	max = Max(corner1, corner2);
}

BoundingBox::~BoundingBox()
{
}

void BoundingBox::intersect(HitRecord& hit, const RenderContext&, const Ray& ray) const
{
  Vector inverse_direction = ray.direction().inverse();
  Vector v1 = (min - ray.origin()) * inverse_direction;
  Vector v2 = (max - ray.origin()) * inverse_direction;
  Vector vmin = Min(v1, v2);
  Vector vmax = Max(v1, v2);
  double near = vmin.maxComponent();
  double far = vmax.minComponent();
  if(near < far && (near > EPSILON || far > EPSILON) && (far < DBL_MAX)) {
	  if(!hit.hit(near))
			  hit.hit(far);
  }
  return;
}
void BoundingBox::extend(const Point& p)
{
  min = Min(p, min);
  max = Max(p, max);
}

void BoundingBox::set_corners(const Point& p1, const Point& p2)
{
	min = Min(p1, p2);
	max = Max(p1, p2);
}

